/**
 * Created with IntelliJ IDEA.
 * User: suntao
 * Date: 16-10-12
 * Time: 下午5:34
 * To change this template use File | Settings | File Templates.
 */
$(function () {
    // 获取图片上传路径
//    var uploadPath = getUploadPath();
    $("#exception_id").hide();
    var device_id = $("#exception_id").text();
//  var type;
//  if (device_id == undefined || device_id == "" || device_id == null || device_id == 0) {
//      // 添加广告
//      type = 1
//  } else {
//      // 编辑广告
//      type = 2;
//  }

//    $("#img_upload").fileinput({
//        showUpload: false,
//        showCaption: false,
//        showPreview: false,
//        browseClass: "btn btn-primary btn-lg",
//        fileType: "any"
////        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
////        allowedFileExtensions: ['jpg', 'png','gif']
//    });

    $("#exception_save").click(function () {
//      var adName = $("#ad_name").val().trim();
//      var clickUrl = $("#ad_click").val().trim();
//      var fileArr = $("#file_upload").val().split('\\');
//      var fileName = fileArr[fileArr.length - 1];
//      var id = $("#device_id").html();
        fileUpload();
    });
    var uploadUrl = "http://localhost:8080/itmaDevice/importSpecialTransDevice.do";
    $("#importFile").fileinput({
        language: 'zh', //设置语言
        uploadUrl: uploadUrl, //上传的地址
        allowedFileExtensions : ['xlsx','xls'],//接收的文件后缀
        showUpload: true, //是否显示上传按钮
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        uploadAsync: false,
        uploadExtraData:function (previewId, index) {
            var obj = {};
            $('#addForm').find('input').each(function() {
                var id = $(this).attr('id'), val = $(this).val();
                obj[id] = val;
            });
            return obj;
        }
    });
}); 
function fileUpload() {
	if ($('input[type="file"]').val() != "") {
		var extend = $('input[type="file"]').val().substr($('input[type="file"]').val().lastIndexOf(".") + 1);
		if ("xls|xlsx".indexOf(extend + "|") != -1) {
			flagPic = false;
            layer.msg("选择的文件必须是EXCEL文件,请确认！", {icon: 1, time: 1000});
		} else {
			$.ajaxFileUpload({
				url: 'importSpecialTransDevice.do?fileType=' + extend,
				//        url : 'adPicUpload.do',
				secureuri: false,
				fileElementId: 'importFile',
				dataType: 'json',
				success: function(data) {
					if (data["code"] == "1000") {
						layer.msg('保存成功!', {
							icon: 1,
							time: 1000
						});
						window.setTimeout("eval(1);", 1000);
						window.parent.location.reload();
					} else {
						layer.msg('保存失败!', {
							icon: 5,
							time: 1000
						});
					} 

				},
				error: function(data) {
					layer.msg('操作失败!', {
						icon: 2,
						time: 1000
					});
				}
			});
		}
	}
}
function getUploadPath () {
    $.ajax({
        url: "adDelete.do",
        type: "post",
        async: true,
        data:"id=" + id,
        dataType: "json",
        success: function (data) {
            if (data["code"] == "1000") {
                location.replace(location.href);
//                $(obj).parents("tr").remove();
//                layer.msg('已删除!', {icon: 1, time: 1000});
            } else {
                layer.msg('删除失败!', {icon: 5, time: 1000});
            }
        },
        error: function (data) {
            layer.msg('操作失败!', {icon: 2, time: 1000});
        }
    });
}